package com.user.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.common.util.StringUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author: stefanie-zy
 * @time: 2024/2/1-9:50
 * @description: 默认属性填充配置类
 */
@Configuration
@Slf4j
@Schema(name = "配置-默认属性填充")
public class StefanieMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 设置新增时间
        if (metaObject.hasSetter("createTime")) {
            Object createTime = getFieldValByName("createTime", metaObject);
            if (StringUtil.isNull(createTime)) {
                this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
                log.info("The default create time has been filled");
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 设置修改时间
        if (metaObject.hasSetter("updateTime")) {
            Object updateTime = getFieldValByName("updateTime", metaObject);
            if (StringUtil.isNull(updateTime)) {
                this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
                log.info("The default update time has been filled");
            }
        }
    }
}
